package com.controller.machine;

import com.common.anntation.Log;
import com.common.emums.BusinessType;
import com.common.utils.EntityConversionUtil;
import com.github.pagehelper.PageInfo;
import com.model.base.PageResult;
import com.model.base.Result;
import com.model.domain.machine.GetMachineModelInfoDO;
import com.model.dto.machine.machineInfo.GetMachineListDTO;
import com.model.dto.machine.machineModelInfo.GetMachineModelListDTO;
import com.model.dto.machine.machineModelInfo.SaveMachineModelInfoDTO;
import com.model.pojo.device.DeviceType;
import com.model.pojo.machine.MachineInfo;
import com.model.pojo.machine.MachineModelInfo;
import com.model.pojo.user.UserInfo;
import com.model.vo.machine.machineInfo.getMachineList.GetMachineListVO;
import com.model.vo.machine.machineModelInfo.getMachineModelList.GetMachineModelListVO;
import com.service.device.DeviceTypeService;
import com.service.machine.MachineModelInfoService;
import com.service.user.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * TODO
 *
 * @Description
 * @Author tm
 * @Date 2024-02-28 21:27
 **/
@Api(tags = {"农机型号管理"})
@Slf4j
@RestController
@RequestMapping("/v1/machine/model")
@RequiredArgsConstructor
public class MachineModelInfoController {
    private final MachineModelInfoService machineModelInfoService;
    private final UserInfoService userInfoService;
    private final DeviceTypeService deviceTypeService;
    @Log(title = "农机型号管理-农机型号列表", businessType = BusinessType.SELECT)
    @ApiOperation(value = "农机型号列表")
    @PostMapping("/getMachineModelList")
    public Result<PageResult<List<GetMachineModelListVO>>> getMachineModelList(@RequestBody @Valid GetMachineModelListDTO dto) {
        List<GetMachineModelListVO> voList = new ArrayList<>();
        GetMachineModelInfoDO getMachineModelInfoDO = EntityConversionUtil.copy(dto,GetMachineModelInfoDO.class);
        List<MachineModelInfo> modelList = machineModelInfoService.getPageList(getMachineModelInfoDO);
        if(modelList!=null && modelList.size()>0){
            modelList.forEach(item ->{
                GetMachineModelListVO vo = EntityConversionUtil.copy(item,GetMachineModelListVO.class);
                vo.setFactoryId(item.getFactoryUserId());
                String factoryName = "";
                UserInfo userInfo = userInfoService.getById(item.getFactoryUserId());
                if(userInfo!=null && userInfo.getName()!=null)factoryName = userInfo.getName();
                vo.setFactoryName(factoryName);
                String dType = "";
                DeviceType deviceType = deviceTypeService.getById(item.getDeviceTypeId());
                if(deviceType!=null)dType = deviceType.getName();
                vo.setDeviceType(dType);
                voList.add(vo);
            });
        }
        PageInfo<MachineModelInfo> pageInfo = new PageInfo<>(modelList);
        PageResult<List<GetMachineModelListVO>> build = PageResult.<List<GetMachineModelListVO>>builder().result(voList).pageInfo(pageInfo).build();
        return Result.suc(build);
    }


    @Log(title = "农机型号管理-保存农机型号信息", businessType = BusinessType.INSERT)
    @ApiOperation(value = "保存农机型号信息")
    @PostMapping("/saveMachineModelInfo")
    public Result<Objects> saveMachineModelInfo(@RequestBody @Valid SaveMachineModelInfoDTO dto) {
        int num = 0;
        if(dto.getModelId()!=null && dto.getModelId()>0){
            MachineModelInfo modelInfo = machineModelInfoService.getById(dto.getModelId());
            if(modelInfo == null){
                return Result.fail("9999","农机类型不存在");
            }
            modelInfo.setDeviceTypeId(dto.getDeviceTypeId());
            modelInfo.setFactoryUserId(dto.getFactoryId());
            modelInfo.setMachineName(dto.getMachineName());
            modelInfo.setModelName(dto.getModelName());
            modelInfo.setEngineCompany(dto.getEngineCompany());
            modelInfo.setEngineModel(dto.getEngineModel());
            modelInfo.setMachineImg(dto.getMachineImg());
            num = machineModelInfoService.update(modelInfo);
        }else{
            MachineModelInfo modelInfo = new MachineModelInfo();
            modelInfo.setDeviceTypeId(dto.getDeviceTypeId());
            modelInfo.setFactoryUserId(dto.getFactoryId());
            modelInfo.setMachineName(dto.getMachineName());
            modelInfo.setModelName(dto.getModelName());
            modelInfo.setEngineCompany(dto.getEngineCompany());
            modelInfo.setEngineModel(dto.getEngineModel());
            modelInfo.setMachineImg(dto.getMachineImg());
            num = machineModelInfoService.save(modelInfo);
        }
        if(num>0){
            return Result.suc();
        }
        return Result.fail("9999","保存失敗");
    }
}
